<?php

namespace app\api\module;


use app\common\model\BrokerComment;
use app\common\model\Contact;
use app\common\model\Notice;
use app\common\model\UserAttention;
use think\facade\Db;

class Chat extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_CHAT_NOTICE_INDEX,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】获取聊天签名
            case ZFN_CHAT_SIGN:
                return $this->chatSign();
            //【1002】消息页面
            case ZFN_CHAT_NOTICE_INDEX:
                return $this->noticeIndex();
            //【1003】通知列表
            case ZFN_CHAT_NOTICE_LIST:
                return $this->noticeList();
            //【1004】通知详情
            case ZFN_CHAT_NOTICE_DETAIL:
                return $this->noticeDetail();
            //【1005】取聊天对方标签
            case ZFN_CHAT_PARTNER_TAG:
                return $this->chatPartnerTag();
            //【1006】联系人列表
            case ZFN_CHAT_CONTACT_LIST:
                return $this->contactList();
            //【1007】联系人资料
            case ZFN_CHAT_CONTACT_INFO:
                return $this->contactInfo();
            //【1008】联系人备注
            case ZFN_CHAT_CONTACT_REMARK:
                return $this->contactRemark();
            //【1009】聊天按钮是否显示
            case ZFN_CHAT_BTN_SHOW:
                return $this->btnShow();
            //【1010】对联系人评价信息
            case ZFN_CHAT_COMMENT_INFO:
                return $this->commentInfo();
            //【1011】对联系人评价提交
            case ZFN_CHAT_COMMENT_SUB:
                return $this->commentSub();
            default:
                send_developer_error("模块【聊天】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function chatSign()
    {
        $user = $this->currentUser();

        $chat = new \app\common\model\Chat();

        $data = [
            'chat_uid' => $chat->getUserChatUid($user['id']),
            'sign' => $chat->getGenUserSig($user['id']),
            'nickname'=>$user['nickname'],
            'avatar'=>manageAvatar($user['avatar'])
        ];
        $this->insertResponse('', $data);
        return $this->response('聊天标识加签名');
    }

    private function noticeIndex()
    {
        $user = $this->currentUser(false);
        $data = Notice::index($user);
        $this->insertResponse('', $data);
        return $this->response('消息页面');
    }

    private function noticeList()
    {
        $user = $this->currentUser(true);
        $type = $this->essentialParam('type');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Notice::noticeList($user, $type, $page, $page_size);
        $this->insertResponse('', $data);
        return $this->response('消息通知列表');
    }

    private function noticeDetail()
    {
        $user = $this->currentUser(true);
        $notice_id = $this->essentialParam('notice_id');
        $source = $this->essentialParam('source');
        $data = Notice::detail($user, $notice_id, $source);
        $this->insertResponse('', $data);
        return $this->response('消息通知文章详情');
    }

    private function chatPartnerTag()
    {
        $user = $this->currentUser(true);
        $chat_uids = $this->essentialParam('chat_uids');
        $data = Contact::userPartnerTag($user, $chat_uids);
        $this->insertResponse('', $data);
        return $this->response('聊天对象标签');
    }

    private function contactList()
    {
        $user = $this->currentUser(true);
        $data = Contact::userContactLists($user);
        $this->insertResponse('data', $data);
        return $this->response('联系人列表');
    }

    private function contactInfo()
    {
        $user = $this->currentUser(true);
        $chat_uid = $this->essentialParam('chat_uid');
        $data = Contact::userContactInfo($user,$chat_uid);
        $this->insertResponse('', $data);
        return $this->response('联系人资料');
    }

    private function contactRemark()
    {
        $user = $this->currentUser(true);
        $chat_uid = $this->essentialParam('chat_uid');
        $remark = $this->optionalParam('remark');
        Contact::addRemark(1,$user['id'],$chat_uid,$remark);
        return $this->response('备注成功');
    }

    private function btnShow()
    {
        $user = $this->currentUser(true);
        $chat_uid = $this->essentialParam('chat_uid');
        $data = Contact::userBtnShow($user,$chat_uid);
        $this->insertResponse('',$data);
        return $this->response('');
    }

    private function commentInfo()
    {
        $user = $this->currentUser(true);
        $chat_uid = $this->essentialParam('chat_uid');
        $data_type = $this->essentialParam('data_type');
        $data = Contact::userCommentInfo($user,$chat_uid,$data_type);
        $this->insertResponse('',$data);
        return $this->response('评价信息');
    }

    private function commentSub()
    {
        $user = $this->currentUser(true);
        $chat_uid = $this->essentialParam('chat_uid');
        $star = $this->essentialParam('star');
        $tag = $this->optionalParam('tag');
        BrokerComment::userCommentSub($user,$chat_uid,$star,$tag);
        return $this->response('评价成功');
    }

}
